package com.example.mycarcalling.service.impl;

import com.example.mycarcalling.bean.Navigation;
import com.example.mycarcalling.dao.ApplicationMapper;
import com.example.mycarcalling.dao.NavigationMapper;
import com.example.mycarcalling.service.NavigationService;
import com.example.mycarcalling.vo.Address;
import com.example.mycarcalling.vo.OrderWithNav;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service("navigationService")
@Transactional(rollbackFor = Exception.class)
public class NavigationServiceImpl implements NavigationService {

    @Resource
    private NavigationMapper navigationMapper;

    @Override
    public Long sendingNavigation(Navigation navigation) {
        if(null==navigation){
            return null;
        }
        Navigation navigation1 = null;
        try {
            navigation1 = navigationMapper.seletReadyOrStartNavigation(navigation.getPublishingUser());
            if(null != navigation1){
                return null;
            }
            navigationMapper.insertSelective(navigation);
            System.out.println("navigation.getNavigationId() :"+navigation.getNavigationId());

            return navigation.getNavigationId();
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public Navigation getNotFinishNavigation(Long publishingUser) {
        if(null == publishingUser || publishingUser == 0){
            return null;
        }
        Navigation navigation = null;
        try {
            navigation = navigationMapper.seletReadyOrStartNavigation(publishingUser);
            return navigation;
        }catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public List<Navigation> getLimitThreeNavigation(Address address,Integer passengerNumberPermit) {
        if(null == address) {
            return null;
        }
        List<Navigation> navigations = null;
        try {
            navigations = navigationMapper.selectPassengerNavigationLimit3(address,passengerNumberPermit);
            return navigations;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public List<OrderWithNav> getDriverAllOrderNav(Long driverId) {
        if(null == driverId){
            return null;
        }
        try {
            return navigationMapper.selectOrderWithNavigation(driverId);
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public List<OrderWithNav> getPassengerAllOrderNav(Long passengerId) {
        if(null == passengerId){
            return null;
        }
        try {
            return navigationMapper.selectOrderWithNavigationP(passengerId);
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public Navigation getLastNavigation(Long lastNvId) {
        return navigationMapper.selectByPrimaryKey(lastNvId);
    }


}
